#ifndef _RFM12TRANSCEIVER_H_
#define _RFM12TRANSCEIVER_H_


#define DDR_IN 0
#define DDR_OUT 1

#define PORT_SEL PORTB
#define PIN_SEL PINB
#define DDR_SEL DDRB

#define PORT_SDI PORTB
#define PIN_SDI PINB
#define DDR_SDI DDRB

#define PORT_SCK PORTB
#define PIN_SCK PINB
#define DDR_SCK DDRB

#define PORT_SDO PORTB
#define PIN_SDO PINB
#define DDR_SDO DDRB

#define PORT_IRQ PORTD
#define PIN_IRQ PIND
#define DDR_IRQ DDRD

#define PORT_DATA PORTD
#define PIN_DATA PIND
#define DDR_DATA DDRD

#define RFXX_SCK 7// |
#define RFXX_SDO 6// |RF_PORT
#define RFXX_SDI 5// |
#define RFXX_SEL 4// |


#define SEL_OUTPUT() DDR_SEL |= (1<<RFXX_SEL)
#define HI_SEL() PORT_SEL|= (1<<RFXX_SEL)
#define LOW_SEL() PORT_SEL&=~(1<<RFXX_SEL)

#define SDI_OUTPUT() DDR_SDI |= (1<<RFXX_SDI)
#define HI_SDI() PORT_SDI|= (1<<RFXX_SDI)
#define LOW_SDI() PORT_SDI&=~(1<<RFXX_SDI)

#define SDO_INPUT() DDR_SDO&= ~(1<<RFXX_SDO)
#define LOW_SDO() PORT_SDO&= (1<<RFXX_SDO)
#define SDO_HI() PIN_SDO&(1<<RFXX_SDO)

#define SCK_OUTPUT() DDR_SCK |= (1<<RFXX_SCK)
#define HI_SCK() PORT_SCK|= (1<<RFXX_SCK)
#define LOW_SCK() PORT_SCK&=~(1<<RFXX_SCK)

#define RF12_IRQ 2
#define IRQ_IN() DDR_IRQ &=~(1<<RF12_IRQ)
#define WAIT_IRQ_LOW() while(PIND&(1<<RF12_IRQ))

#define RF12_DATA 4//PD4
#define DATA_OUT() DDR_DATA|=1<<RF12_DATA
#define HI_DATA() PORT_DATA|=1<<RF12_DATA


#define STATE_RFM12_RECV_IDLE   0x00
#define STATE_RFM12_RECV        0x01
#define STATE_RFM12_RECV_CHK    0x02
#define STATE_RFM12_TRANS_FRAME 0x10
#define STATE_RFM12_TRANS_DATA  0x11
#define STATE_RFM12_TRANS_END   0x12


#if defined _RFM12TRANS_C_
  #define _EXT_
#else
  #define _EXT_ extern
#endif

_EXT_ volatile t_rfm12 rfm12;

_EXT_ void RFXX_PORT_INIT(void);
_EXT_ unsigned int RFXX_WRT_CMD(unsigned int aCmd);
_EXT_ void RF12_INIT(void);
_EXT_ void RF12_SEND(unsigned char aByte);

_EXT_ void rfm12_init(void);
_EXT_ void rfm12_reset_FIFO(void);
_EXT_ unsigned char rfm12_send(T_CMD *cmd, unsigned int uiLen);
_EXT_ void rfm12_send_sequence(void);

#undef _EXT_
#endif
